<?php
defined( 'BASEPATH' ) or exit( 'No direct script access allowed' );
class Guestbook_handle extends MY_Controller
{
    function get_list()
    {
        $begin = ( int ) $this->input->get( 'begin' );
        $length = ( int ) $this->input->get( 'length' );
        
        $begin = $begin < 0 ? 0 : $begin;
        $length = $length < 1 ? 10 : $length;
        
        $this->load->model( 'guestbook_model' );
        $count = $this->guestbook_model->get_list_count();
        if ($begin > 0 && $begin >= $count)
        {
            $this->rs->err( 4 );
        }
        $list = $this->guestbook_model->get_list( $length, $begin );
        $this->rs->ok( array (
                'record_count' => $count,
                'list' => $list 
        ) );
    }
    function get()
    {
        $id = ( int ) $this->input->get( 'id' );
        $this->load->model( 'guestbook_model' );
        if ($content = $this->guestbook_model->get( $id ))
        {
            if (! $content['status'])
            {
                $this->guestbook_model->update( array (
                        'status' => 1 
                ), $id );
            }
            if ($content['content'])
            {
                $content['content'] = htmlspecialchars( $content['content'] );
                $content['content'] = str_replace( array (
                        "\r\n",
                        "\r",
                        "\n" 
                ), '<br/>', $content['content'] );
                $content['content'] = str_replace( array (
                        ' ',
                        '　' 
                ), '&nbsp;', $content['content'] );
            }
            $this->rs->ok( $content );
        }
        $this->rs->err( '内容不存在！' );
    }
    function modify()
    {
        $id = ( int ) $this->input->post( 'id' );
        if ($id <= 0)
        {
            $this->rs->err( 2 );
        }
        
        $modify_data = array ();
        
        $deleted = $this->input->post( 'deleted' );
        if (isset( $deleted ))
        {
            $modify_data['deleted'] = $deleted ? 1 : 0;
        }
        
        if (empty( $modify_data ))
        {
            $this->rs->err( 2 );
        }
        $this->load->model( 'guestbook_model' );
        if (false !== $this->guestbook_model->update( $modify_data, $id ))
        {
            $this->rs->ok();
        }
        $this->rs->err();
    }
}